@extends('layouts.admin_common') @section('content')
@if(app('env')=='production')
<script src="{{ secure_asset('js/corp.js') }}"></script>
@else
<script src="{{ asset('js/corp.js') }}"></script>
@endif
<div class="breadcrumbs_container">
	{{Breadcrumbs::render('admin.corp_master') }}</div>
<div class="system_tab_line"></div>
<script type="text/javascript">
var delStaffUrl="<?php echo url('admin/corp/delete_staff');?>";
var getStaffUrl="<?php echo url('admin/corp/getStaffs');?>";
function saveUser(){
	if ($("#staff_name").val()==""){
		alert("担当者名を入力してください。");
		//$('#myAlert').modal('show');
		return;
	}
	if ($("#staff_kana").val()==""){
		alert("担当者カナを入力してください。");
		//$('#myAlert').modal('show');
		return;
	}
	 var formData = new FormData();
	 if ($("#staff_id") != undefined && $("#staff_id").val()  != undefined) {
		 formData.append("staff_id", $("#staff_id").val());
	 }

     formData.append("staff_name", $("#staff_name").val());
     formData.append("staff_kana", $("#staff_kana").val());
     formData.append("staff_tel", $("#staff_tel").val());
     formData.append("staff_mail", $("#staff_mail").val());
     formData.append("corp_id",$("#corp_id").val());

     if ($("#send_bill_mail").prop("checked") == true) {
    	 formData.append("send_bill_mail", 1);
     } else {
    	 formData.append("send_bill_mail", 0);
     }
     if ($("#send_sale_mail").prop("checked") == true) {
    	 formData.append("send_sale_mail", 1);
     } else {
    	 formData.append("send_sale_mail", 0);
     }
     if ($("#send_transfer_mail").prop("checked") == true) {
    	 formData.append("send_transfer_mail", 1);
     } else {
    	 formData.append("send_transfer_mail", 0);
     }

	 $.ajax({
        headers: {
        	'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        },
        type: 'POST',
        url: '../corp/save_staff' ,
        data: formData ,
        processData:false,
        mimeType:"multipart/form-data",
        contentType: false,
        cache: false,
        success:function(data){
        	$('#addUserModel').modal('hide');
        	getStaffs();
        },
        error:function(err){

        	  $('#addUserModel').modal('hide');
        	  getStaffs();
        }
    });
}
function deleteStaff(staffId) {
	if (confirm("担当者を削除してもよろしいですか。")) {
		var formData = new FormData();
		formData.append("staff_id", staffId);

    	$.ajax({
            headers: {
            	'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            },
            type: 'POST',
            url: '../corp/delete_staff' ,
            data: formData ,
            processData:false,
            mimeType:"multipart/form-data",
            contentType: false,
            cache: false,
            success:function(data){
            	getStaffs();
            	//$('#addUserModel').modal('hide');
            },
            error:function(err){
            	getStaffs();
            }
        });
	}
}
function addStaff(){
	$("#staff_id").val("");
	$("#staff_name").val("");
	$("#staff_kana").val("");
	$("#staff_tel").val("");
	$("#staff_mail").val("");
	$("#send_bill_mail").prop("checked", false);
	$("#send_sale_mail").prop("checked", false);
	$("#send_transfer_mail").prop("checked", false);
	$('#addUserModel').modal('show');
}
function editStaff(id, name, kana, tel, mail, bill,sale,trans){
	$("#staff_id").val(id);
	$("#staff_name").val(name);
	$("#staff_kana").val(kana);
	$("#staff_tel").val(tel);
	$("#staff_mail").val(mail);
	if(bill) {
		$("#send_bill_mail").prop("checked", true);
	} else {
		$("#send_bill_mail").prop("checked", false);
	}
	if(sale) {
		$("#send_sale_mail").prop("checked", true);
	} else {
		$("#send_sale_mail").prop("checked", false);
	}
	if(bill) {
		$("#send_bill_mail").prop("checked", true);
	} else {
		$("#send_bill_mail").prop("checked", false);
	}
	if(trans) {
		$("#send_transfer_mail").prop("checked", true);
	} else {
		$("#send_transfer_mail").prop("checked", false);
	}
	$('#addUserModel').modal('show');
}
function getStaffs(){
	var corp_id = $("#corp_id").val();
	$.post(getStaffUrl,
		  { corp_id: corp_id},
	  function(data){
	    //リクエストが成功した際に実行する関数
	    //alert("Data Loaded: " + data);
	    //var staffs = $.parseJSON(data);
	    var str="<table class='table table-condensed' ><tr><td>担当者名</td><td>担当者カナ</td><td>担当者TEL</td><td>担当者MAIL</td><td>請求書</td><td>セール情報</td><td>発送案内</td><td></td><td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td></tr>";
	    $.each(data, function(index, value) {
		    var row ="<tr><td>"+value.staff_name+"</td><td>"+value.staff_kana+"</td><td>"+value.tel+"</td><td>"+value.mail+"</td><td>";
		    if(value.send_bill_mail) {
			    row+="●";
			}
		    row+="</td><td>";
		    if(value.send_sale_mail) {
			    row+="●";
			}
		    row+="</td><td>";
		    if(value.send_transfer_mail) {
			    row+="●";
			}
		    row += "</td><td>";
		    row += "<input type='button' value='修正' class='btn btn-primary button_normal' onclick=\"editStaff('";
		    row += value.staff_id + "','" + value.staff_name + "','" + value.staff_kana + "','"+value.tel + "','" + value.mail + "','" + value.send_bill_mail+"','"+value.send_sale_mail+"','"+value.send_transfer_mail+"')\" />";
			row += "<input type='button' value='削除' class='btn btn-primary button_normal' onclick=\"deleteStaff('"+value.staff_id+"')\"/></td></tr>";
		    "</td></tr>";
		    str+=row;
	    });
	    str+="</table>";
	    $("#stafftable").html(str);
	  }
	);
}
$(function(){
	getStaffs();
});
</script>
<script src="https://ajaxzip3.github.io/ajaxzip3.js" charset="UTF-8"></script>

<body>
	<div id="outDiv" style="margin-bottom: 40px;">
		<form id="compForm" class="form-inline" method="post">
			<table class="table table-condensed">
				<col width="160px">
				<col width="*">
				<col width="160px">
				<col width="*">
				<tr>
					<td>会社名</td>
					<td><input type="hidden" id="corp_id" name="CorpMaster[corp_id]"
						value="{{$allInfo[0]->corp_id}}"> <input type="text"
						class="form-control input-lg" placeholder="会社名"
						style="width: 95%; margin-right: 5px;"
						name="CorpMaster[corp_name]" value="{{$allInfo[0]->corp_name}}"> *</td>
					<td>ログインID</td>
					<td>
				<?php if($allInfo[0]->corp_id!==""):?>
				{{$allInfo[1]->login_id}}
				<input type="hidden" class="form-control" placeholder=""
						name="userMaster[user_id]" value="{{$allInfo[1]->user_id}}">
				<?php endif;?>
				</td>
				</tr>
				<tr>
					<td>会社名カナ</td>
					<td><input type="text" class="form-control" placeholder="会社名カナ"
						name="CorpMaster[corp_catelogue]"
						style="width: 95%; margin-right: 5px;"
						value="{{$allInfo[0]->corp_catelogue}}"> *</td>

					<td>ログインＰass</td>
					<td>
				<?php if($allInfo[0]->corp_id!==""):?>
				<input type="text" class="form-control" placeholder=""
						name="userMaster[password]" value="{{$allInfo[1]->password}}" />
						*
				<?php endif;?>
				</td>
				</tr>
				<tr>
					<td>郵便番号</td>
					<td><input type="text" class="form-control" placeholder="郵便番号"
						onblur="AjaxZip3.zip2addr(this,'','CorpMaster[corp_adress]','CorpMaster[corp_adress]');"
						checked="checked" name="CorpMaster[corp_postcode]"
						id="corpPostCode" value="{{$allInfo[0]->corp_postcode}}"
						style="width: 120px;"> *数字、"-"を組み合わせて入力してください</td>
				</tr>
				<tr>
					<td>住所</td>
					<td><input type="text" class="form-control" placeholder="住所"
						checked="checked" name="CorpMaster[corp_adress]"
						value="{{$allInfo[0]->corp_adress}}"
						style="width: 95%; margin-right: 5px;"> *</td>
				</tr>
				<tr>
					<td>住所（ビル名）</td>
					<td><input type="text" class="form-control" placeholder=""
						style="width: 95%; margin-right: 5px;"
						name="CorpMaster[buil_name]" value="{{$allInfo[0]->buil_name}}"> *</td>
				</tr>
				<tr>
					<td>会社TEL</td>
					<td><input type="text" class="form-control" placeholder="会社TEL"
						checked="checked" name="CorpMaster[corp_tel]" id="corpTel"
						value="{{$allInfo[0]->corp_tel}}"> *数字、"-"を組み合わせて入力してください</td>
					<td>会社FAX</td>
					<td><input type="text" class="form-control" placeholder="会社FAX"
						checked="checked" name="CorpMaster[corp_fax]" id="corpFax"
						value="{{$allInfo[0]->corp_fax}}"> *数字、"-"を組み合わせて入力してください</td>
				</tr>
				<tr>
					<td>代表Mail</td>
					<td><input type="text" class="form-control" placeholder="代表Mail"
						checked="checked" name="CorpMaster[corp_mail]" id="corpMail"
						value="{{$allInfo[0]->corp_mail}}"> *</td>
				</tr>
				<tr>
					<td>代表担当者</td>
					<td><input type="text" class="form-control" placeholder="代表担当者"
						name="CorpMaster[corp_stuff]" value="{{$allInfo[0]->corp_stuff}}"></td>
					<td>担当者TEL</td>
					<td><input type="text" class="form-control" placeholder="担当者TEL"
						name="CorpMaster[corp_stuff_telephone]" id="corpStuffTel"
						value="{{$allInfo[0]->corp_stuff_telephone}}" value=""></td>
				</tr>
				<tr>
					<td>担当者カナ</td>
					<td><input type="text" class="form-control" placeholder="担当者カナ"
						name="CorpMaster[corp_stuff_kana]"
						value="{{$allInfo[0]->corp_stuff_kana}}"></td>
					<td>担当Ｍail</td>
					<td><input type="text" class="form-control" placeholder="担当Ｍail"
						name="CorpMaster[corp_stuff_mail]" id="corpStuffMail"
						value="{{$allInfo[0]->corp_stuff_mail}}"></td>
				</tr>
				<tr>
					<td>請求方法</td>
					<td><select name="CorpMaster[bill_method]" id="bill_method"
						class="form-control">
						<?php if($allInfo[0]->bill_method ==1 ):?>
							<option value="1" selected>月締め</option>
							<option value="2">都度</option>
						<?php elseif($allInfo[0]->bill_method ==2 ):?>
							<option value="1" >月締め</option>
							<option value="2" selected>都度</option>
						<?php else:?>
							<option value="1" >月締め</option>
							<option value="2">都度</option>
						<?php endif;?>
					</select></td>
					<td>端数処理</td>
					<td><select name="CorpMaster[round_flg]" class="form-control">
					<?php if($allInfo[0]->round_flg == 1 ):?>
							<option value="1" selected>四捨五入</option>
							<option value="2">切り捨て</option>
							<option value="3">切り上げ</option>
						<?php elseif($allInfo[0]->round_flg == 2 ):?>
							<option value="1" >四捨五入</option>
							<option value="2" selected>切り捨て</option>
							<option value="3">切り上げ</option>
						<?php elseif($allInfo[0]->round_flg == 3 ):?>
							<option value="1" >四捨五入</option>
							<option value="2" >切り捨て</option>
							<option value="3" selected>切り上げ</option>
						<?php else:?>
							<option value="1" >四捨五入</option>
							<option value="2" >切り捨て</option>
							<option value="3">切り上げ</option>
						<?php endif;?>
					</select></td>
				</tr>
			</table>
			<?php if ($allInfo[0]->corp_id!=="" ):?>
			<table class="table table-condensed">
				<tr>
					<td>担当者</td>
					<td><input type="button" value="追加"
						class="btn btn-primary button_normal" data-toggle="modal"
						onclick="addStaff()" /></td>
				</tr>
			</table>

			<div id="stafftable">
				<table class="table table-condensed">
					<tr>
						<td>担当者名</td>
						<td>担当者カナ</td>
						<td>担当者TEL</td>
						<td>担当者MAIL</td>
						<td>請求書</td>
						<td>セール情報</td>
						<td>発送案内</td>
						<td></td>
						<td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
					</tr>
					<tr>@foreach ($staffInfos as $staffInfo)


					<tr>
						<td>{{$staffInfo->staff_name}}</td>
						<td>{{$staffInfo->staff_kana}}</td>
						<td>{{$staffInfo->tel}}</td>
						<td>{{$staffInfo->mail}}</td>
						<td><?php if($staffInfo->send_bill_mail){echo "●";}?></td>
						<td><?php if($staffInfo->send_sale_mail){echo "●";}?></td>
						<td><?php if($staffInfo->send_transfer_mail){echo "●";}?></td>
						<td><input type="button" value="修正"
							class="btn btn-primary button_normal"
							onclick="editStaff('{{$staffInfo->staff_id}}','{{$staffInfo->staff_name}}','{{$staffInfo->staff_kana}}','{{$staffInfo->tel}}','{{$staffInfo->mail}}','{{$staffInfo->send_bill_mail}}','{{$staffInfo->send_sale_mail}}','{{$staffInfo->send_transfer_mail}}')" />
							<input type="button" value="削除"
							class="btn btn-primary button_normal"
							onclick="deleteStaff('{{$staffInfo->staff_id}}')" /></td>
					</tr>
					@endforeach
				</table>
			</div>
				<?php else:?>
				<div style="width: 100%; margin-bottom: 20px;">
				<span>*会社を登録してから担当者を登録することが可能です。</span>
			</div>
			<br>
<?php endif;?>
<div>
				<table border="0" cellspacing="0" style="width: 1300px;">
					<tr>
						<td><input type="button" value="保存"
							class="pure-button pure-button-primary button_normal"
							onclick="onSubmit('create')" /></td>
						<td><input type="button" value="キャンセル"
							class="pure-button pure-button-primary button_normal"
							onclick="location.href='../home'" /></td>
						<td><input type="button" value="削除"
							class="pure-button pure-button-primary button_normal"
							onclick="onSubmit('delete')" /></td>
						<!-- 						<td><input type="button" value="Csv取込"
 							class="pure-button pure-button-primary button_normal"
							onclick="onSubmit('export')" /></td>-->

				</table>
			</div>
			<div>@if(isset($success)) {{$success}} @endif</div>
			<div>@if(isset($error)) {{$error}} @endif</div>
			<input type="hidden" name="_token" id="csrf-token"
				value="{{ Session::token() }}" />
		</form>
	</div>
	<div class="modal fade" id="addUserModel" tabindex="-1" role="dialog"
		aria-labelledby="addUserLabel" aria-hidden="true">
		<div class="modal-dialog" role="document">
			<div class="modal-content">
				<div class="modal-header">
					<h5 class="modal-title" id="addUserLabel">担当者追加</h5>
				</div>
				<div class="modal-body">
					<table class="table table-condensed">
						<tr>
							<td>担当者名</td>
							<td><input type="hidden" name="staff_id" id="staff_id"> <input
								type="text" class="form-control" placeholder="ユーザ名"
								name="staff_name" checked="checked" id="staff_name"></td>
						</tr>
						<tr>
							<td>担当者カナ</td>
							<td><input type="text" class="form-control" placeholder="ユーザカナ"
								id="staff_kana" checked="checked"></td>
						</tr>
						<tr>
							<td>担当者TEL</td>
							<td><input type="text" class="form-control" placeholder="TEL"
								id="staff_tel"></td>
						</tr>
						<tr>
							<td>担当者MAIL</td>
							<td><input type="text" class="form-control" placeholder="MAIL"
								name="staff_mail" id="staff_mail"></td>
						</tr>
						<tr>
							<td>請求書</td>
							<td><input type="checkbox" class="form-control"
								id="send_bill_mail" style="width: 20px;" /></td>
						</tr>
						<tr>
							<td>セール情報</td>
							<td><input type="checkbox" class="form-control"
								id="send_sale_mail" style="width: 20px;" /></td>
						</tr>
						<tr>
							<td>発送案内</td>
							<td><input type="checkbox" class="form-control"
								id="send_transfer_mail" style="width: 20px;" /></td>
						</tr>
					</table>
				</div>

				<div class="modal-footer">
					<button type="button" class="btn btn-secondary"
						data-dismiss="modal">閉じる</button>
					<button type="button" class="btn btn-primary" onclick="saveUser()">保存</button>
				</div>
			</div>
		</div>
	</div>

</body>
</html>
@endsection
